package cn.shangqizhenshuai.sqiot.mqtt;

import cn.shangqizhenshuai.sqiot.Serivce.EquipmentService;
import cn.shangqizhenshuai.sqiot.Serivce.HiValueService;
import cn.shangqizhenshuai.sqiot.entity.Equipment;
import cn.shangqizhenshuai.sqiot.entity.HiValue;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * 设置回调函数 接收 采集设备的值存储在数据库中由触发器来插入到最新运行数据表中
 */
@Repository("myMqttCallBack")
public class MyMqttCallBack implements MqttCallback {

    @Autowired
    private HiValueService hiValueService;
    @Autowired
    private EquipmentService equipmentService;


    public void connectionLost(Throwable cause) {

    }

    public void messageArrived(String topic, MqttMessage message) throws Exception {
        //消息的 TODO 收到消息发送到数据库 建立历史表 和 当前最新表
        HiValue hiValue = new HiValue();
        hiValue.setSqiotDatatime(new Date());
        hiValue.setSqiotTopic(topic);
        String s = message.toString();
        hiValue.setSqiotValue(Double.parseDouble(s));
        Equipment like_by_statrurl = equipmentService.get_like_by_statrurl(topic);
        hiValue.setEid(like_by_statrurl.getId());
        Integer add = hiValueService.add(hiValue);
    }
    public void deliveryComplete(IMqttDeliveryToken token) {

    }
}
